﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace Snake
{
    class Painter
    {
        private readonly char gameNodeSymbol;
        private readonly char frameChar;

        public Painter(char gameNodeChar, char frameSymbol)
        {
            gameNodeSymbol = gameNodeChar;
            frameChar = frameSymbol;
        }

        public Painter(char frameSymbol)
        {
            frameChar = frameSymbol;
        }

        public void DrawNode(ConsoleNode node)
        {
            ClearGameNode(node);
            DrawGameNode(node);
        }

        public void DrawGameNode(ConsoleNode node)
        {
            Console.ForegroundColor = node.ColorNode;
            Console.SetCursorPosition(node.X, node.Y);
            Console.WriteLine(node.GameNodeSymbol);
            Thread.Sleep(10);
            Console.ForegroundColor = ConsoleColor.White;
        }
        public void ClearGameNode(ConsoleNode node)
        {
            Console.SetCursorPosition(node.X, node.Y);
            Console.WriteLine(' ');
        }
        public void DraweGameFieldFrame(GameField field)
        {
            int yPosition = field.StartY - 1;
            int xPosition = field.StartX - 1;

            for (int i = 0; i < field.Height + 2; i++)
            {
                Console.SetCursorPosition(xPosition, yPosition);
                for (int j = 0; j < field.Width + 2; j++)
                {
                    if (i == 0 || i == field.Height + 1)
                    {
                        Console.Write(frameChar);
                    }
                    else if (j == 0 || j == field.Width + 1)
                    {
                        Console.Write(frameChar);
                    }
                    else
                    {
                        Console.Write(' ');
                    }
                }
                Console.WriteLine();
                Console.SetCursorPosition(field.StartX, ++yPosition);
            }
        }

        public void DrawNodes(ConsoleNode node)
        {
            Console.ForegroundColor = node.ColorNode;
            Console.SetCursorPosition(node.X, node.Y);
            Console.WriteLine(node.GameNodeSymbol);
            Console.ForegroundColor = ConsoleColor.White;
        }

        public void DrawNodeList(IEnumerable<ConsoleNode> nodes)
        {
            foreach (var consoleNode in nodes)
            {
                DrawNodes(consoleNode);
            }
        }

        public void DrawScore(GameField field, int shift, int score, int roundNumber)
        {
            Console.SetCursorPosition(field.StartX + field.Width + 1 + shift, field.StartY);
            Console.WriteLine("Round: {0}", roundNumber);
            Console.SetCursorPosition(field.StartX + field.Width + 1 + shift, field.StartY + 1);
            Console.Write("Score: {0}", score);
            
        }

        public void DrawScore(GameField field, int shift, int score)
        {
            Console.SetCursorPosition(field.StartX + field.Width + 1 + shift, field.StartY);
            Console.Write("Score: {0}", score);

        }
    }
}
